/**
 * 大拇指点赞功能
 */
import {
    count,
} from './myMath.js';
import {
    myPromise,
} from './myPromise.js';
/*import {
    myAxios,
} from './myAxios.js';
*/
class ThumbUp {
    constructor(ele, numEle) {
        this.ele = ele;
        this.numEle = numEle;
        this.currNum = this.getDefaultNum();
    }
    clickEvent() {
        // this.ele.click(this.clickThump());
        this.clickThump()();
    }
    removeAnimation() {
        this.ele.removeClass(this.getScale());
        this.numEle.removeClass(this.getAddOne());
    }
    setAnimation() {
        this.ele.addClass(this.getScale());
        this.numEle.addClass(this.getAddOne());
    }
    getScale() {
        return 'thumbScale';
    }
    getAddOne() {
        return 'addOne';
    }
    getDefaultNum() {
        return 0;
    }
    getInterval() {
        return 1000;
    }
}
class MyThumb extends ThumbUp {
    constructor(ele, numEle) {
        super(ele, numEle);
        this.visitor = `visitor${Math.floor((Math.random() * 100))}`;
    }
    clickThump() {
        let lastClickTime;
        return (...agrs) => {
            let currTime = (new Date).getTime();
            // 本次点击时间在周期内，不提交
            if (lastClickTime && currTime - lastClickTime < this.getInterval()) {
                alert('亲，点赞过于频繁啊，稍后点赞吧！');
                return;
            }
            lastClickTime = currTime;
            // 将大拇指放大，旁边显示+1
            this.setAnimation();
            this.currNum = count(this.currNum);
            // 保存本次点赞
            this.saveThumbUpNum();
            setTimeout(() => {
                this.removeAnimation();
            }, this.getInterval());
        }
    }
    saveThumbUpNum() {
        // method1：使用Promise，依赖$.ajax
        /*myPromise({
            ajax: $.ajax,
            url: '/index/update',
            type: 'POST',
            params: {
                creator: this.visitor
            }
        }).then(() => {
            console.log('第 ${this.currNum} 次点赞成功！');
        }, (error) => {
            console.error('第 ${this.currNum} 次点赞失败：${error}');
            this.removeAnimation();
        });*/
        // method2：使用axios，不依赖其他组件，很纯
        /*myAxios({
            baseURL: 'http://127.0.0.1:3000/'
        }).get(`../index/update/${this.visitor}`).then(() => {
            console.log(`第 ${this.currNum} 次点赞成功！`);
        }).catch((error) => {
            console.error(`第 ${this.currNum} 次点赞失败：${error}`);
            this.removeAnimation();
        });*/
        // post方式不行。有bug
        /*myAxios({
            baseURL: 'http://localhost:3000/',
            method: 'POST',
        }).post('../index/update', `creator=${this.visitor}`, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        }).then(() => {
            console.log(`第 ${this.currNum} 次点赞成功！`);
        }).catch((error) => {
            console.error(`第 ${this.currNum} 次点赞失败：${error}`);
            this.removeAnimation();
        });*/
    }
}
export default MyThumb;